using Microsoft.AspNetCore.Mvc;

namespace APMService
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            builder.Services.AddLogging();

            builder.Services.AddSingleton<TemporalClient>(await TemporalClient.ConnectAsync(new TemporalClientConnectOptions("localhost:7233")));
            builder.Services.AddPooledDbContextFactory<AppDbContext>(options => options.UseNpgsql("Host=localhost;Port=5432;Username=temporal;Password=temporal;"));
            builder.Services.AddScoped<IAutoPreventiveMaintenanceService, AutoPreventiveMaintenanceService>();
            builder.Services.AddSingleton<APMActivities>();
            builder.Services.AddHostedService<WorkerService>();
            builder.Services.AddExceptionHandler<GlobalExceptionHandler>();

            var app = builder.Build();
            app.UseExceptionHandler(_ => { });

            app.MapPost("/group", async (IAutoPreventiveMaintenanceService apmservice, [FromBody] OperateGroupRequest req) =>
            {
                if (req.GroupId != 0)
                {
                    return Results.BadRequest("GroupId should be 0.");
                }
                var group = await apmservice.CreateGroup(req);

                var resp = new GroupResponse
                {
                    GroupId = group.Id,
                    Group = group
                };
                return Results.Ok(resp);
            });

            app.MapGet("/group/{group_id}", async (IAutoPreventiveMaintenanceService apmservice, [FromRoute] int group_id) =>
            {
                var group = await apmservice.GetGroup(group_id);
                if (group != null)
                {
                    var resp = new GroupResponse
                    {
                        GroupId = group.Id,
                        Group = group
                    };
                    return Results.Ok(resp);
                }
                return Results.NotFound();
            });

            app.MapGet("/test/{group_id}", async (IAutoPreventiveMaintenanceService apmservice, [FromRoute] int group_id) =>
            {
                await apmservice.TestWorkflow(group_id);
                return Results.Ok();

            });

            app.MapGet("/groups", async (IAutoPreventiveMaintenanceService apmservice) =>
            {
                var result = await apmservice.GetAllGroups();
                return Results.Ok(result);
            });

            app.MapPut("/group", async (IAutoPreventiveMaintenanceService apmservice, [FromBody] OperateGroupRequest req) =>
            {
                var result = await apmservice.UpdateGroup(req);
                return result ? Results.Ok() : Results.BadRequest();
            });

            app.MapDelete("/group/{group_id}", async (IAutoPreventiveMaintenanceService apmservice, [FromRoute] int group_id) =>
            {
                var result = await apmservice.DeleteGroup(group_id);
                return result ? Results.Ok() : Results.NotFound();
            });

            app.Run();
        }
    }
}
